<?php

namespace app\nideshop\controller;

use think\Db;
use app\nideshop\service\Token;

class Footprint extends Base
{
	protected $datas;
	protected $table = 'footprint';
	/**
	 * 足迹列表
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-28T23:00:39+0800
	 * @return   [type]                   [description]
	 */
	public function listAction()
	{
		$userId = Token::unlock_url($_SERVER['HTTP_X_NIDESHOP_TOKEN']);
		$list = Db::name($this->table)->alias('f')
			->join('goods g', 'f.goods_id=g.id', 'LEFT')
			->where('f.user_id', $userId)
			->field('f.*,g.name,g.list_pic_url,g.goods_brief,g.retail_price')
			->order('f.id', 'desc')
			->paginate(10, false, ['page' => 1]);
		return json([
			'errno' => 0,
			'errmsg' => '',
			'data' => [
				'count' => $list->total(),
				'totalPages' => 0,
				'pageSize' => 10,
				'currentPage' => $list->currentPage(), 
				'data' => $list->items()
			]
		]);	
	}

	/**
	 * 删除足迹
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-28T23:26:08+0800
	 * @return   [type]                   [description]
	 */
	public function deleteAction()
	{
		$this->datas = $this->params;
		//验证
		$footprintId = $this->datas['footprintId'];
		$userId = Token::unlock_url($_SERVER['HTTP_X_NIDESHOP_TOKEN']);
		$goods = Db::name($this->table)->where(['user_id' => $userId, 'id' => $footprintId])->find();
		$result = Db::name($this->table)
			->where(['user_id' => $userId, 'goods_id' => $goods['goods_id']])
			->delete();
		if (!$result) {
			return josn(['errno' => 400, 'errmsg' => '删除失败', 'data' => []]);
		}	
		return josn(['errno' => 0, 'errmsg' => '删除成功', 'data' => []]);
	}
}

